using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Konst3d.Efficiency.Suite;
using Konst3d.Efficiency.Suite.SparsedBinary;

namespace DevUI
{
    public partial class DailyReportForm : Form
    {
        class ReportListEntry
        {
            public IMonitor Monitor;
            public IStatisticDay Day;

            public override string ToString()
            {
                return Monitor.Title;
            }
        }

        private IEfficiencySuite _suite;

        public DailyReportForm()
        {
            InitializeComponent();
        }

        public void ShowReport(IEfficiencySuite suite)
        {
            _suite = suite;

            CreateReport(DateTime.Now);

            Show();
        }

        private void CreateReport(DateTime date)
        {
            uiMonitorsTextBox.Items.Clear();

            foreach (var monitor in _suite.Monitors)
            {
                if (monitor.Active &&
                    monitor.ActiveDays.DaysOfWeek[(int)date.DayOfWeek] &&
                    monitor.ActiveDays.DaysOfMonth[date.Day - 1]/* &&
                    monitor.Statistic[date].Active*/) // not active, Created < date !!!
                {
                    uiMonitorsTextBox.Items.Add(new ReportListEntry() { Monitor = monitor, Day = monitor.Statistic[date] });
                }
            }
        }

        private void uiMonitorsTextBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var entry = uiMonitorsTextBox.SelectedItem as ReportListEntry;

            if (entry != null)
            {
                uiActiveCheckBox.Checked = entry.Day.Active;
                uiValueUpDown.Value = (decimal)entry.Day.Value;
                uiCommentaryTextBox.Text = entry.Day.Commentary;
            }
        }

        private void uiSaveButton_Click(object sender, EventArgs e)
        {
            var entry = uiMonitorsTextBox.SelectedItem as ReportListEntry;

            if (entry != null)
            {
                entry.Day.Active = uiActiveCheckBox.Checked;
                entry.Day.Value = (float)uiValueUpDown.Value;
                entry.Day.Commentary = uiCommentaryTextBox.Text;
            }
        }

        // shwo report for new date
        private void uiCallendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            CreateReport(e.Start);
        }

        private void DailyReportForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }
    }
}
